var exceptionHandle = {
    //继续生产接口入参
    continueProductParams:{
        dealStatus:'',//处理状态
        orderId:'',//订单号
        customerCode:'',//客户编码
        customerId:'',//客户id
        customerName:'',//客户名称
        dealReason:'',// 异常处理原因
        dealUserId:'',//处理人id
        dealUserName:'',//处理人姓名
        subChannelProjectInfo: {
            channelProjectId:'',//渠道项目编码(对方)
            channelProjectName:'',//渠道项目名称(对方)
        }
    },
    // 提交接口的入参
    submitParams:{
        dealStatus:'',//处理状态
        orderId:'',//订单号
        dealReason:'',//异常处理原因
        dealUserId:'',//处理人id
        dealUserName:'',// 处理人姓名
    },
    parentCustomerCode:'',
    projectInfoList: [],
    projectDepartmentId: '',

    init:function(){
        if(getQueryString("orderId")){
            var orderId = getQueryString("orderId");
            var isCheck = getQueryString("isCheck");
            if (getQueryString("abnormalType")) {
                if (getQueryString("abnormalType") == 10) {
                    $('.project-name-wrap').hide();
                }
            }
            if (isCheck) {
                $('.handle-area').hide();
                $('#handleStatus').attr('disabled', true);
                $('.detail-content input').attr('disabled', true);
                $('.detail-content textarea').attr('disabled', true);
                $('#projectName').attr('disabled',true);
            }
            this.getDataInfo(orderId);
        }
        this.addClick();
    },
    addClick:function(){
        // 所属客户联系搜索
        $('#belongCustomer').on('keyup',function(){
            var value = $('#belongCustomer').val();
            exceptionHandle.belongCustomerInfo(value);
            $('.belongCustomerLists').show();
        });
        // 所属客户某一项
        // 客户名称某一项
        $('.belongCustomerLists').delegate('li','click',function(){
            var itemLi = $(this).text();
            $('#belongCustomer').val(itemLi);
            exceptionHandle.continueProductParams.customerCode =  $(this).attr('data-code');
            exceptionHandle.continueProductParams.customerId =  $(this).attr('data-id');
            exceptionHandle.continueProductParams.customerName = itemLi;
            $('.belongCustomerLists').hide();
            exceptionHandle.getProjectNameList($(this).attr('data-project'));
        });
        $(document).on('click',function(){
            $('.belongCustomerLists').hide();
        });
        // 处理状态
        $('#handleStatus').change(function(){
            var status = $(this).children('option:selected').val();
            if(status == 20){//无需处理
                $('#submitButton').show();
                $('#continueProductButton').hide();
                $('#belongCustomer').attr('disabled' ,true)
            }else if(status == 10){
                $('#submitButton').hide();
                $('#continueProductButton').show();
                $('#belongCustomer').attr('disabled',false)
            }
        })

        // 取消
        $('#cancelButton').unbind().on('click',function(){
            parent.layer.closeAll();
        });
        // 订单继续生产
        var isclickbtn =true
        $('#continueProductButton').unbind().on('click',function(){
            if(isclickbtn){
            var dealStatus = $('#handleStatus').find('option:selected').val();
            var projectCode = $('#projectName').find('option:selected').val();
            var dealReason = $('#dealReason').val();
            var belongCustomer = $('#belongCustomer').val();//所属客户
            if(!belongCustomer){
                layer.msg('请输入所属客户',{offset:'t'});
                return false
            }
            //  if(!dealReason){
            //      layer.msg('请输入处理原因',{offset:'t'});
            //      return false
            //  }
            exceptionHandle.continueProductParams.dealStatus = dealStatus;
            exceptionHandle.continueProductParams.dealReason = dealReason;
            exceptionHandle.continueProductParams.dealUserId = JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID;
            exceptionHandle.continueProductParams.dealUserName = JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name;
            if (projectCode && getQueryString("abnormalType") == 20) {
                if (exceptionHandle.projectInfoList.length > 0) {
                    var currentItem = exceptionHandle.projectInfoList.filter(function(item) {
                        return item.channelProjectId == projectCode;
                    })[0];
                    exceptionHandle.continueProductParams.subChannelProjectInfo = {
                        channelProjectId: currentItem.channelProjectId,
                        channelProjectName: currentItem.channelProjectName,
                    }
                    var msg = '确认将'+ exceptionHandle.projectDepartmentId +'部门编码，插入至'+ currentItem.channelProjectName +'对方项目名称作为该项目的部门？';

                    layer.confirm(msg, {
                        btn:['确认','取消'],
                        title:'提示',
                        offset:'t'
                    },function(index){
                        exceptionHandle.continueProduct();
                        isclickbtn = false
                        layer.close(index)
                    })
                }
            } else {
                exceptionHandle.continueProduct()
                isclickbtn = false
            }
        }
    setTimeout(function () {
        isclickbtn = true
        },500)
        });
        //提交
        $('#submitButton').unbind().on('click',function(){
            var dealStatus = $('#handleStatus').find('option:selected').val();
            var dealReason = $('#dealReason').val();
            // if(!dealReason){
            //     layer.msg('请输入处理原因',{offset:'t'});
            //     return false
            // }

            exceptionHandle.submitParams.dealStatus = dealStatus;
            exceptionHandle.submitParams.dealReason = dealReason;
            exceptionHandle.submitParams.dealUserId = JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID;
            exceptionHandle.submitParams.dealUserName = JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name;

            layer.confirm('无需处理后，该订单将被取消，确认取消该订单吗？',{
                btn:['确认','取消'],
                title:'提交提示',
                offset:'t'
            },function(index){
                exceptionHandle.submitInfo();
                layer.close(index)
            })

        })

    },
    getDataInfo:function(orderId){
        var params = {
            orderId:orderId
        }
        var successData = function(data){
            if(data.errorCode == 0){
                if(data.data){
                    var result = data.data.order;
                    var result2 = data.data.preAddressReceiverList;
                    var obj = {
                        channelName:result.parentCustomerName,//渠道
                        orderCode:result.orderId,
                        belongCustomer: result.customerName,//所属客户
                        provinceName:result.receiverProvinceName,//省
                        cityName:result.receiverCityName,
                        townName:result.receiverTownName,
                        areaName:result.receiverAreaName,
                        detailAddress:result.receiverDetailAddress?result.receiverDetailAddress:'',
                        mobile:result2[0].mobile,
                        receiverName:result2[0].receiverName,
                    }
                    exceptionHandle.projectDepartmentId = data.data.abnormalBasicsInfo.projectDepartmentId;
                    // 继续生产的接口入参
                    exceptionHandle.continueProductParams.orderId = result.orderId;
                    exceptionHandle.continueProductParams.customerCode = result.customerCode;
                    exceptionHandle.continueProductParams.customerId = result.customerId;
                    exceptionHandle.continueProductParams.customerName = result.customerName;
                    // 提交的接口入参
                    exceptionHandle.submitParams.orderId = result.orderId;
                    exceptionHandle.parentCustomerCode = result.parentCustomerCode;
                    exceptionHandle.renderDetail(obj);
                    if (data.data.abnormalBasicsInfo) {
                        exceptionHandle.renderBaseInfo(data.data.abnormalBasicsInfo);
                    }
                    exceptionHandle.renderInvoiceInfo(result);
                    // if (data.data.abnormalBasicsInfo.channelProjectId) {
                    //     exceptionHandle.getCustomerProjectId(result.customerName, data.data.abnormalBasicsInfo.channelProjectId);
                    // }
                    exceptionHandle.getCustomerProjectId(result.customerName, data.data.abnormalBasicsInfo.channelProjectId);
                }
            }
        }
        return web.doAjax(root.dockingplatform.queryPreOrderAbnormal, 'post', params, successData, 2);
    },
    // 渲染处理信息
    renderDetail:function(data){
        var channelName = data.channelName;//渠道
        var orderCode = data.orderCode;
        var belongCustomer = data.belongCustomer;
        var receiveAddress = data.provinceName + data.cityName + data.areaName + data.townName + data.detailAddress + ' '+ data.mobile +' '+ data.receiverName;

        $('#channelName').text(channelName);
        $('#orderCode').text(orderCode);
        $('#belongCustomer').val(belongCustomer);
        $('#receiveAddress').text(receiveAddress);
    },
    // 继续生产接口
    continueProduct:function(){
        var successData = function(data){
            if(data.errorCode == 0){
                layer.msg('继续生产成功', {icon: 6, time: 1000});
                setTimeout(function () {
                    var index = parent.location.reload();
                    parent.layer.close(index)
                }, 1000);
            }else{
                layer.msg(data.msg,{offset:'t'})
            }
        }
        return web.doAjax(root.dockingplatform.continuePlacedOrder, 'post', exceptionHandle.continueProductParams, successData, 1);
    },
    // 提交接口
    submitInfo:function(){
        var successData = function(data){
            if(data.errorCode == 0){
                layer.msg('提交成功', {icon: 6, time: 1000});
                setTimeout(function () {
                    var index = parent.location.reload();
                    parent.layer.close(index)
                }, 1000);
            }else{
                layer.msg(data.msg,{offset:'t'})
            }
        }
        return web.doAjax(root.dockingplatform.cancelPlacedOrder, 'post', exceptionHandle.submitParams, successData, 1);
    },
    // 所属客户搜索结果
    belongCustomerInfo:function(value){
        var params = {
            customerName:value,
            parentCustomerCode:exceptionHandle.parentCustomerCode
        }
        var successData = function(data){
            if(data.errorCode == 0){
                var result = data.data;
                if(result && result.length >0){
                    var channelItem =  '';
                    result.forEach(function(item,index){
                        channelItem +="<li class='clearfix pl-10 mb-5' data-project='"+item.customerCode+"' data-code='"+item.customerCode+"' data-id='"+item.customerId+"'>"+item.customerName+"</li>"
                    });
                    $('.belongCustomerLists').html(channelItem)
                }else{
                    var empty = '<li class="clearfix pl-10 mb-5">无数据</li>';
                    $('.belongCustomerLists').html(empty)
                }
            }
        }
        return web.doAjax(root.dockingplatform.seachSubChanelConfigByName, 'post', params, successData, 1);
    },
    // 查询所属客户 projectId
    getCustomerProjectId: function(customerName, channelProjectId) {
        var params = {
            customerName,
            parentCustomerCode:exceptionHandle.parentCustomerCode
        }
        var successData = function(data){
            if(data.errorCode == 0){
                var result = data.data;
                if (result && result.length > 0) {
                    var projectId = result[0].customerCode;
                    exceptionHandle.getProjectNameList(projectId, channelProjectId);  // 获取对方项目名称列表
                }
            }
        }
        return web.doAjax(root.dockingplatform.seachSubChanelConfigByName, 'post', params, successData, 1);
    },
    // 
    getProjectNameList:function(projectId, channelProjectId){
        var params = {
            customerCode: projectId,
            parentCustomerCode: exceptionHandle.parentCustomerCode,
        }
        var successData = function(data){
            if(data.errorCode == 0){
                var result = data.data;
                exceptionHandle.projectInfoList = result;
                var emptyOption = '<option value="">请选择</option>';
                if(result && result.length > 0) {
                    var projectItem =  '';
                    result.forEach(function(item){
                        projectItem +='<option value='+item.channelProjectId+'>'+item.channelProjectName+'</option>';
                    });
                    $('#projectName').html(emptyOption + projectItem);
                    // 回显对方项目名称
                    if (channelProjectId) {
                        $('#projectName').val(channelProjectId);
                    }
                } else {
                    $('#projectName').html(emptyOption);
                }
            }
        }
        return web.doAjax(root.dockingplatform.seachProjectNameList, 'post', params, successData, 1);
    },
    // 格式化表格内容
    formatTableContent: function(ele) {
        if (ele != 0 && !ele) {
            return '';
        }
        return ele;
    },
    // 渲染基础信息
    renderBaseInfo: function(data) {
        var baseInfoHtml = '<tr>'+
            '<td class="col-3-l"><span class="f-l">渠道客户名称：</span><span>'+ this.formatTableContent(data.channelProjectName) +'</span></td>'+
            '<td class="col-3-1"><span class="f-l">渠道客户编码：</span><span>'+ this.formatTableContent(data.channelProjectId) +'</span></td>'+
            '<td class="col-3-1"><span class="f-l">渠道部门名称：</span><span>'+ this.formatTableContent(data.channelDepartmentName) +'</span></td>'+
            '</tr>'+
            '<tr>'+
            '<td class="col-3-l"><span class="f-l">渠道部门编码：</span><span>'+ this.formatTableContent(data.channelDepartmentId) +'</span></td>'+
            '<td class="col-3-1"><span class="f-l">业 务 员：</span><span>'+ this.formatTableContent(data.salesManagerName) +'</span></td>'+
            '<td class="col-3-1"><span class="f-l">客户归属：</span><span>'+ this.formatTableContent(data.organizationName) +'</span></td>'+
            '</tr>'+
            '<tr>'+
            '<td class="col-3-l"><span class="f-l">下单时项目/部门编码：</span><span>'+ this.formatTableContent(data.projectDepartmentId) +'</span></td>'+
            '</tr>';
        $('#base-info-table').html(baseInfoHtml);
    },
    // 渲染发票信息
    renderInvoiceInfo: function(data) {
        var billingMethod = '';
        var invoiceType = '';

        // 1随单开票/2集中开票
        if (data.billingMethod) {
            switch (data.billingMethod) {
                case 1:
                    billingMethod = '随单开票';
                    break;
                case 2:
                    billingMethod = '集中开票';
                    break;
                default:
                    billingMethod = data.billingMethod;
            };
        } else {
            billingMethod = '';
        }

        // 1-增值税普通发票 2-增值税专⽤发票
        if (data.type) {
            switch (data.type) {
                case 1:
                    invoiceType = '增值税普通发票';
                    break;
                case 2:
                    invoiceType = '增值税专用发票';
                    break;
                default:
                    invoiceType = data.type;
            };
        } else {
            invoiceType = '';
        }
        var invoiceTitleType=""
        if(data.invoiceTitleType==1){
            invoiceTitleType="个人"
        }else if(data.invoiceTitleType==2){
            invoiceTitleType="单位"
        }
        var invocieInfoHtml = '<tr>'+
            '<td class="col-3-l"><span class="f-l">开票方式：</span><span>'+ billingMethod +'</span></td>'+
            '<td class="col-3-1"><span class="f-l">发票类型：</span><span>'+ invoiceType +'</span></td>'+
            '<td class="col-3-1"><span class="f-l">发票抬头：</span><span>'+ this.formatTableContent(data.title) +'</span></td>'+
            '</tr>'+
            '<tr>'+
            '<td class="col-3-1"><span class="f-l">纳税人识别号：</span><span>'+ this.formatTableContent(data.enterpriseTaxpayer) +'</span></td>'+
            '<td class="col-3-l"><span class="f-l">开户银行：</span><span>'+ this.formatTableContent(data.bank) +'</span></td>'+
            '<td class="col-3-1"><span class="f-l">银行账户：</span><span>'+ this.formatTableContent(data.account) +'</span></td>'+
            '</tr>'+
            '<tr>'+
            '<td class="col-3-1"><span class="f-l">联系电话：</span><span>'+ this.formatTableContent(data.tel) +'</span></td>'+
            '<td class="col-3-1"><span class="f-l">联系地址：</span><span>'+ this.formatTableContent(data.address) +'</span></td>'+
            '<td class="col-3-1"><span class="f-l">抬头类型：</span><span>'+ invoiceTitleType +'</span></td>'+
            '</tr>'+
            '<tr>'+
            '<td class="col-3-1"><span class="f-l">开票明细：</span><span>'+ this.formatTableContent(data.invoiceDetails) +'</span></td>'+
            '<td class="col-3-1"><span class="f-l">收票人姓名：</span><span>'+ this.formatTableContent(data.invoiceRecipientName) +'</span></td>'+
            '<td class="col-3-1"><span class="f-l">收票人单位：</span><span>'+ this.formatTableContent(data.invoiceRecipientCompany) +'</span></td>'+
            '</tr>'+
            '<tr>'+
            '<td class="col-3-1"><span class="f-l">收票人电话：</span><span>'+ this.formatTableContent(data.invoiceRecipientTel) +'</span></td>'+
            '<td class="col-3-1"><span class="f-l">收票人邮箱：</span><span>'+ this.formatTableContent(data.invoiceRecipientEmail) +'</span></td>'+
            '<td class="col-3-1"><span class="f-l">收票地址：</span><span>'+ this.formatTableContent(data.invoiceRecipientAddress) +'</span></td>'+
            '</tr>';
        $('#invoice-info-table').html(invocieInfoHtml);
    },

}
exceptionHandle.init()